package com.wink_172.library

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import com.wink_172.library.LocalService.MyBinder
import com.wink_172.library.callback.ICallback
import com.wink_172.library.utils.CommonUtils
import org.xutils.common.Callback.CancelledException
import org.xutils.common.Callback.CommonCallback
import org.xutils.x
import java.io.File

/**
 * 不需和Activity交互的本地服务
 * https://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html
 */
class LocalService : Service() {
    private val mBinder: IBinder = MyBinder()

    inner class MyBinder : Binder() {
        open  var mCallback: ICallback? = null
        val binder: MyBinder
            get() = this

        fun startDownload() {
            Log.e(TAG, "====>>startDownload() ")

            //执行具体的下载任务

            if(mCallback!=null){
//                mCallback!!.onSendEvent()//下载进度回调
            }
        }

        /**
         * 批量上传
         */
        fun startUpload() {
            Log.e(TAG, "====>>startUpload() ")
            //执行具体的上传任务
            val params = CommonUtils.commonRequestParams("url")
            val file = File("path")
            //        Bitmap bitmap = ImageUtil.compressImageFromFile(avater);//压缩图片方式2（第2次压缩）
//        if (bitmap != null) {
//            byte[] bytes = ImageUtil.bitmapToBytes(bitmap);
//            if (bytes != null) {
//
//                String avatar = Base64.encodeToString(bytes, Base64.DEFAULT);
//                params.addBodyParameter("avatar_image_mime_type", "png");
//                params.addBodyParameter("avatar_image_base64", "data:image/png;base64," + avatar);
//            }
//        }
            params.isMultipart = true
            params.addBodyParameter("file", file, null) // 如果文件没有扩展名, 最好设置contentType参数
            val commonCallback: CommonCallback<*> = object : CommonCallback<String?> {
                override fun onSuccess(result: String?) {}
                override fun onError(ex: Throwable, isOnCallback: Boolean) {
                    CommonUtils.showToast(ex.toString())
                    Log.e(TAG, "onError: ====>>$ex")
                }

                override fun onCancelled(cex: CancelledException) {}
                override fun onFinished() {}
            }
            x.http().post(params,commonCallback)
        }
    }
/**
 * 执行顺序start
 * */
    override fun onCreate() {
        Log.e(TAG, "====>>onCreate")
        super.onCreate()
    }
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        Log.e(TAG, "====>>Received start id $startId: $intent")
        // We want this service to continue running until it is explicitly
// stopped, so return sticky.

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                // 开始执行后台任务
//            }
//        }).start();
        return START_STICKY
    }
    override fun onDestroy() {
        Log.e(TAG, "====>>onDestroy")
        super.onDestroy()
    }

    override fun onStart(intent: Intent, startId: Int) {
        Log.e(TAG, "====>>onStart")
        super.onStart(intent, startId)
    }



    override fun onBind(intent: Intent): IBinder? {
        Log.e(TAG, "onBind: ====>>")
        return mBinder
    }
    /**
     * 执行顺序end
     * */
    companion object {
        private const val TAG = "LocalService"
    }
}



